package com.ftqh.project.travel.security.service;

import com.ftqh.project.travel.security.entity.User;
import com.ftqh.project.travel.security.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2016/12/27
 * \* Time: 14:32
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserRepository repository;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Override
    public User save(User user) {
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        repository.save(user);
        return user;
    }
    @Override
    public Optional<User> findByUsername(String username){
        return repository.findByUsername(username);
    }
    @Override
    public Optional<User> findByPhone(String phone){
        return repository.findByPhone(phone);
    }
}
